const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//验证金额 2位小数
const validPrice = val => {
  var prc = /^\d+(\.\d{1,2})?$/
  return prc.test(val)
}

//扩展对象
const extendObj = (obj, objE) => {
  var newObj = cloneObj(obj)
  for (var k in objE)
    newObj[k] = cloneObj(objE[k])
  return newObj
}

//对象转url字符串
const urlencode = (data) => {
  var _result = [];
  for (var i in data) {
    if (typeof data[i] == 'object') {
      for (var j in data[i]) {
        _result.push(i + "_" + j + "=" + data[i][j]);
      }
    } else {
      _result.push(i + "=" + data[i]);
    }
  }

  return _result.join('&');
}

//复制对象方法
const cloneObj = oldObj => {
  if (typeof(oldObj) != 'object')
    return oldObj
  if (oldObj == null)
    return oldObj
  var newObj = new Object()
  for (var k in oldObj)
    newObj[k] = cloneObj(oldObj[k])
  return newObj
}

// 显示繁忙提示
var showBusy = text => wx.showToast({
  title: text,
  icon: 'loading',
  duration: 5000
})

// 显示成功提示
var showSuccess = (text, fn) => wx.showToast({
  title: text,
  icon: 'success',
  success: fn
})

// 显示失败提示
var showError = (text, fn)  => wx.showToast({
  title: text,
  image: '/images/deta_btn_dele.png',
  success: fn
})

// 显示确认按钮的模态框
var showModal = content => {
  wx.hideToast();
  wx.showModal({
    content: content,
    showCancel: false,
    confirmText: '好哒'
  })
}

// 显示成功提示
var hideToast = () => wx.hideToast()


module.exports = {
  formatTime,
  validPrice,
  extendObj,
  formatTime,
  showBusy,
  showSuccess,
  showModal,
  showError,
  hideToast,
  urlencode
}